package logic

import (
	"context"
	"errors"
	"shell/model/mysql"

	"shell/shellrpc/internal/svc"
	"shell/shellrpc/shellrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type AppointmentPingLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAppointmentPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppointmentPingLogic {
	return &AppointmentPingLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AppointmentPingLogic) AppointmentPing(in *shellrpc.AppointmentRequest) (*shellrpc.AppointmentResponse, error) {
	// todo: add your logic here and delete this line

	var home mysql.Home
	mysql.DB.Debug().Where("id=?", in.HomeId).Find(&home)
	if home.ID == 0 {
		return nil, errors.New("当前房源不存在，请更换")
	}

	m := mysql.Home{
		Model: gorm.Model{ID: home.ID},
	}

	err := mysql.DB.Debug().Create(&m).Error
	if err != nil {
		return nil, err
	}

	return &shellrpc.AppointmentResponse{
		HomeId: int64(m.ID),
	}, nil
}
